package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPolicy;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.TransformationtConnetionEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.ControlFlow;
import edu.pku.sei.transformation.editor.views.figures.ConnectionFigure;

public class ControlFlowEditpart extends TransformationLinkEditPart {

	public ControlFlowEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		ConnectionFigure figure = (ConnectionFigure) getFigure();
		figure.setLabel("<<control flow>>", ConnectionFigure.CONNECTION_FIGURE_MIDDLE);
		if (ModelPackage.CONTROLFLOW_GUARD.equals(propertyName)) {
			figure.setLabel(event.getNewValue().toString(), ConnectionFigure.CONNECTION_FIGURE_SOURCE);
		} else {
			super.handlePropertyChanged(event);
		}
	}

	@Override
	protected void refreshVisuals() {
		ConnectionFigure figure = (ConnectionFigure) getFigure();
		super.refreshVisuals();
		figure.setLabel(((ControlFlow)getLink().getModelElement()).getGuard(), ConnectionFigure.CONNECTION_FIGURE_SOURCE);
	}

	@Override
	protected IFigure createFigure() {
		ConnectionFigure figure = new ConnectionFigure(SWT.LINE_SOLID);
		figure.setTooltipLabel("control flow");
		figure.setTargetDecoration(ConnectionFigure.ARROW);
		figure.setLabel("<<control flow>>", ConnectionFigure.CONNECTION_FIGURE_MIDDLE);
		return figure;
	}
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new TransformationtConnetionEditPolicy());
	}

	protected PolylineConnection getConnection() {
		return (PolylineConnection) getFigure();
	}

}
